// pages/marketOrder/component/detailView.js
import Toast from '@vant/weapp/toast/toast';
import Dialog from '@vant/weapp/dialog/dialog';

const {formatDate} = require("../../../utils/util");
const network = require("../../../utils/network");
const util = require("../../../utils/util");
import Validate from "../../../utils/validate";

const app = getApp()

Component({
    /**
     * 组件的属性列表
     */
    properties: {
        create: {
            type: Boolean,
            value: false
        },
        marketOrderId: {
            type: Number,
            value: -1
        }
    },
    ready() {
        util.showLoading('加载中')
        Promise.all([this.initCompanyArr(), this.initCustomer(), this.initUserArr()]).then(() => {
            util.hideLoading()
        }).catch(() => {
            util.showToast('加载失败', 'error')
            util.hideLoading()
        })
        this.initValidate()
        this.initValidatePhotoRequire()
        this.selectComponent('.flow-btn-group').callbackBeforeConfirm = () => {
            return this.validForm()
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        activeNames: ['1'],
        cuForm: {
            fill_date: formatDate(new Date()),
            dept_id: null,
            customer_id: null,
            contract_id: null,
            bill_sale_id: null,
            need_sample: null,
            latest_up_date: null,
            latest_up_time: null,
            latest_down_date: null,
            latest_down_time: null,
            first_time: null,
            has_new_pic: null,
            show_proxy_flag: null,
            pic_need_recycle: null,
            design_user_id: null,
            memo: null,
            mediaType2RequireList: {
                "-1": [],
                "2": [
                    {
                        shoot_type: null,
                        shoot_date: null,
                        baidu_time: null,
                        watermark_time: null,
                        newspaper_time: null,
                        normal_shoot: null,
                        background_clear: null,
                        newpaper_clear: null,
                        close_shoot_num: null,
                        far_shoot_num: null
                    }
                ]
            },
            type: 1,
            companyName: null,// 公司名称
            customer_name: null,// 项目名称
            contractName: null,// 合同名称
            saleOrderName: null,// 销售合同名称
            userName: null,// 用户名称
        },
        dialogShow: false,
        photoRequirementsDataArr: [],
        customer_arr: [],
        selectCustomerLoading: true,
        company_arr: [],
        selectCompanyLoading: true,
        userList: [],
        selectUserLoading: true,
        photoRequire: {
            media_type_id: null,
            media_type_name: null,
            shoot_type: null,
            shoot_date: null,
            baidu_time: null,
            watermark_time: null,
            newspaper_time: null,
            normal_shoot: null,
            background_clear: null,
            newpaper_clear: null,
            close_shoot_num: null,
            far_shoot_num: null,
            memo: null
        },
        edit: false,
        contractList: [],
        selectContractLoading: true,
        selectContractShow: false,
        saleOrderList: [],
        selectSaleOrderLoading: true,
        selectSaleOrderShow: false,
        params: {},
        dateShow: false,
        dateKey: '',
        media_type_arr: [],
        shootDateShow: false,
        beforeClose: (e) => {
            return e === 'cancel'
        },
        defaultDate: null,
        validate: {},
        validatePhotoRequire: {},
        user_id: wx.getStorageSync('user_id'),
        mustIncludeBillSaleId: ''
    },

    /**
     * 组件的方法列表
     */
    methods: {
        // 验证函数
        initValidate() {
            const rules = {
                customer_id: {
                    required: true
                },
                type: {
                    required: true
                },
                contract_id: {
                    required: true,
                },
                dept_id: {
                    required: true
                },
                bill_sale_id: {
                    required: true
                },
                need_sample: {
                    required: true
                },
                latest_up_date: {
                    required: true
                },
                latest_up_time: {
                    required: true
                },
                latest_down_date: {
                    required: true
                },
                latest_down_time: {
                    required: true
                },
                first_time: {
                    required: true
                },
                has_new_pic: {
                    required: true
                },
                show_proxy_flag: {
                    required: true
                },
                pic_need_recycle: {
                    required: true
                },
                design_user_id: {
                    required: true
                },
            }
            const messages = {
                customer_id: {
                    required: '请选择项目'
                },
                type: {
                    required: '请选择市场单类型'
                },
                contract_id: {
                    required: '请选择合同',
                },
                dept_id: {
                    required: '请选择公司名称'
                },
                bill_sale_id: {
                    required: '请选择销售单'
                },
                need_sample: {
                    required: '请选择是否需要小样'
                },
                latest_up_date: {
                    required: '请选择最晚上刊日期'
                },
                latest_up_time: {
                    required: '请选择最晚上刊时间'
                },
                latest_down_date: {
                    required: '请选择最晚下刊日期'
                },
                latest_down_time: {
                    required: '请选择最晚下刊时间'
                },
                first_time: {
                    required: '请选择是否第一次上画'
                },
                has_new_pic: {
                    required: '请选择是否有新画面'
                },
                show_proxy_flag: {
                    required: '请选择是否显示代理资源标识'
                },
                pic_need_recycle: {
                    required: '请选择画面是否回收'
                },
                design_user_id: {
                    required: '请选择设计人员'
                },
            }
            this.data.validate = new Validate(rules, messages)
        },
        // dialog验证
        initValidatePhotoRequire() {
            const rules = {
                media_type_id: {
                    required: true,
                },
                shoot_type: {
                    required: true,
                },
                shoot_date: {
                    required: true,
                }
            }
            const messages = {
                media_type_id: {
                    required: '请选择媒体类型',
                },
                shoot_type: {
                    required: '请选择类型',
                },
                shoot_date: {
                    required: '请选择时间',
                }
            }
            this.data.validatePhotoRequire = new Validate(rules, messages)
        },
        onChange(e) {
            this.setData({
                activeNames: e.detail
            })
        },
        onChange2(e) {
            const key = 'photoRequirementsDataArr[' + e.target.dataset.index + '].activeNames'
            this.setData({
                [key]: e.detail
            })
        },
        onChange3(e) {
            const key = 'photoRequirementsDataArr[' + e.target.dataset.index + '].requireList[' + e.target.dataset.i + '].activeNames'
            this.setData({
                [key]: e.detail
            })
        },
        checkRadioChange(e) {
            let key = 'photoRequire.' + e.target.dataset.key
            this.setData({
                [key]: e.detail
            })
        },
        onClose() {
            this.setData({
                dialogShow: false,
                photoRequire: {
                    shoot_type: null,
                    shoot_date: null,
                    baidu_time: null,
                    watermark_time: null,
                    newspaper_time: null,
                    normal_shoot: null,
                    background_clear: null,
                    newpaper_clear: null,
                    close_shoot_num: null,
                    far_shoot_num: null,
                    memo: null
                }
            })
        },
        showDialog() {
            this.setData({
                dialogShow: true
            })
        },
        selectCustomerConfirm(e) {
            this.setData({
                'cuForm.customer_id': e.detail.value.id,
                'cuForm.customer_name': e.detail.value.name,
                'cuForm.contract_id': null,
                'cuForm.contractName': null,
                'cuForm.bill_sale_id': null,
                'cuForm.saleOrderName': null,
            })
            this.initContractArr(e.detail.value.id)
        },
        selectCompanyConfirm(e) {
            this.setData({
                'cuForm.dept_id': e.detail.value.id,
                'cuForm.companyName': e.detail.value.head,
                selectCompanyShow: false
            })
        },
        // 获取客户待选项
        initCustomer() {
            const that = this
            return new Promise((resolve, reject) => {
                network.reqPromise('GET', {
                    uri: app.api.customerList,
                    params: {}
                }).then(res => {
                    that.setData({
                        customer_arr: res.data,
                        selectCustomerLoading: false
                    })
                    resolve()
                }).catch(() => {
                    reject()
                })
            })
        },
        // 获取公司待选项
        initCompanyArr() {
            const that = this
            return new Promise((resolve, reject) => {
                network.reqPromise('GET', {
                    uri: app.api.companyList,
                    params: {},
                }).then(res => {
                    that.setData({
                        company_arr: res.data,
                        selectCompanyLoading: false
                    })
                    resolve()
                }).catch(() => {
                    reject()
                })
            })
        },
        // 获取设计人员待选项
        initUserArr() {
            const that = this
            return new Promise((resolve, reject) => {
                network.reqPromise('GET', {
                    uri: app.api.designList,
                    params: {}
                }).then(res => {
                    that.setData({
                        userList: res.data,
                        selectUserLoading: false
                    })
                    resolve()
                }).catch(() => {
                    reject()
                })
            })
        },
        // 获取合同待选项
        initContractArr(id) {
            const that = this
            network.GET({
                uri: app.api.contractListByCustomerId,
                params: {customerId: id},
                success(res) {
                    res.data.forEach(item => {
                        item.name = item.part_a + '——' + item.contract_num
                    })
                    that.setData({
                        contractList: res.data,
                        selectContractLoading: false
                    })
                },
                fail(e) {
                    console.log(e)
                }
            })
        },
        // 获取销售单待选项
        initSaleOrderArr(id, mustIncludeBillSaleId, type) {
            const that = this
            network.GET({
                uri: app.api.saleOrderListByContractId,
                params: {
                    contractId: id,
                    mustIncludeBillSaleId: mustIncludeBillSaleId,
                    type: type
                },
                success(res) {
                    res.data.forEach(item => {
                        item.name = item.contract_num + '——' + item.actual_execute_time_start + '~' + item.actual_execute_time_end
                    })
                    that.setData({
                        saleOrderList: res.data,
                        selectSaleOrderLoading: false
                    })
                },
                fail(e) {
                    console.log(e)
                }
            })
        },
        // 获取销售单中的媒体类型
        getMediaTypeArrBySaleId(id) {
            const that = this
            network.reqPromise('GET', {
                uri: app.api.getMediaTypeArrBySaleId,
                params: {
                    id: id,
                },
            }).then(res => {
                let arr = [{
                    id: -1,
                    name: '全部'
                }]
                arr = arr.concat(res.data)
                this.setData({
                    media_type_arr: arr
                })
            }).catch(() => {

            })
        },
        selectUserConfirm(e) {
            this.setData({
                'cuForm.userName': e.detail.value.name,
                'cuForm.design_user_id': e.detail.value.id,
            })
        },
        onDateConfirm(e) {
            let key = 'cuForm.' + this.data.dateKey
            this.setData({
                [key]: formatDate(e.detail),
                dateKey: '',
                dateShow: false,
            })
        },
        onDateClose() {
            this.setData({
                dateKey: '',
                defaultDate: null,
                dateShow: false,
            })
        },
        showCalendar(e) {
            if (this.data.cuForm.flow_status === 3 || this.data.cuForm.flow_status === 4) {
                return
            }
            let key = e.target.dataset.key
            let now = new Date()
            if (this.data.cuForm[key]) {
                now = new Date(this.data.cuForm[key])
            }
            this.setData({
                dateShow: true,
                defaultDate: now.getTime(),
                dateKey: e.target.dataset.key
            })
        },
        selectContractConfirm(e) {
            this.setData({
                'cuForm.contract_id': e.detail.value.id,
                'cuForm.contractName': e.detail.value.name,
                'cuForm.bill_sale_id': null,
                'cuForm.saleOrderName': null,
            })
            this.initSaleOrderArr(e.detail.value.id, this.data.mustIncludeBillSaleId, 1)
        },
        selectSaleOrderConfirm(e) {
            this.setData({
                'cuForm.bill_sale_id': e.detail.value.id,
                'cuForm.saleOrderName': e.detail.value.name,
            })
            this.getMediaTypeArrBySaleId(e.detail.value.id)
        },
        onShootDateConfirm(e) {
            this.setData({
                shootDateShow: false,
                'photoRequire.shoot_date': formatDate(e.detail)
            })
        },
        showShootDate() {
            let now = new Date()
            if (this.data.photoRequire.shoot_date) {
                now = new Date(this.data.photoRequire.shoot_date)
            }
            this.setData({
                shootDateShow: true,
                defaultDate: now.getTime(),
            })
        },
        onShootDateClose() {
            this.setData({
                defaultDate: null,
                shootDateShow: false
            })
        },
        radioChange(e) {
            if (e.target.dataset.key === 'photoRequire.media_type_id') {
                var tmp = this.data.media_type_arr.find(item => item.id === e.detail);
                this.setData({
                    'photoRequire.media_type_name': tmp.name
                })
            }
            this.setData({
                [e.target.dataset.key]: e.detail,
            })
        },
        inputChange(e) {
            this.setData({
                [e.target.dataset.key]: e.detail
            })
        },
        removePhoto(e) {
            let index1 = e.target.dataset.index
            let index2 = e.target.dataset.i
            let index3 = e.target.dataset.n
            this.data.photoRequirementsDataArr[index1].requireList[index2].arr.splice(index3, 1)
            if (this.data.photoRequirementsDataArr[index1].requireList[index2].arr.length === 0) {
                this.data.photoRequirementsDataArr[index1].requireList.splice(index2, 1)
            }
            if (this.data.photoRequirementsDataArr[index1].requireList.length === 0) {
                this.data.photoRequirementsDataArr.splice(index1, 1)
            }
            this.setData({
                photoRequirementsDataArr: this.data.photoRequirementsDataArr,
            })
        },
        onConfirm() {
            if (!this.data.validatePhotoRequire.checkForm(this.data.photoRequire)) {
                Toast.fail({
                    message: this.data.validatePhotoRequire.errorList[0].msg,
                    context: this,
                    selector: '#custom-selector'
                })
            } else {
                var find = this.data.photoRequirementsDataArr.find(item => item.media_type_id === this.data.photoRequire.media_type_id);
                if (find) {
                    var require = find.requireList.find(tmp => tmp.type === this.data.photoRequire.shoot_type);
                    if (require) {
                        require.arr.push({
                            shoot_type: this.data.photoRequire.shoot_type,
                            shoot_date: this.data.photoRequire.shoot_date,
                            baidu_time: this.data.photoRequire.baidu_time,
                            watermark_time: this.data.photoRequire.watermark_time,
                            newspaper_time: this.data.photoRequire.newspaper_time,
                            normal_shoot: this.data.photoRequire.normal_shoot,
                            background_clear: this.data.photoRequire.background_clear,
                            newpaper_clear: this.data.photoRequire.newpaper_clear,
                            close_shoot_num: this.data.photoRequire.close_shoot_num,
                            far_shoot_num: this.data.photoRequire.far_shoot_num,
                            memo: this.data.photoRequire.memo
                        })
                    } else {
                        find.requireList.push({
                            type: this.data.photoRequire.shoot_type,
                            name: this.data.photoRequire.shoot_type === 1 ? '上刊' : this.data.photoRequire.shoot_type === 2 ? '下刊' : '中间监播',
                            arr: [
                                {
                                    shoot_type: this.data.photoRequire.shoot_type,
                                    shoot_date: this.data.photoRequire.shoot_date,
                                    baidu_time: this.data.photoRequire.baidu_time,
                                    watermark_time: this.data.photoRequire.watermark_time,
                                    newspaper_time: this.data.photoRequire.newspaper_time,
                                    normal_shoot: this.data.photoRequire.normal_shoot,
                                    background_clear: this.data.photoRequire.background_clear,
                                    newpaper_clear: this.data.photoRequire.newpaper_clear,
                                    close_shoot_num: this.data.photoRequire.close_shoot_num,
                                    far_shoot_num: this.data.photoRequire.far_shoot_num,
                                    memo: this.data.photoRequire.memo
                                }
                            ]
                        })
                    }
                } else {
                    this.data.photoRequirementsDataArr.push({
                        media_type_id: this.data.photoRequire.media_type_id,
                        media_type_name: this.data.photoRequire.media_type_name,
                        requireList: [
                            {
                                type: this.data.photoRequire.shoot_type,
                                name: this.data.photoRequire.shoot_type === 1 ? '上刊' : this.data.photoRequire.shoot_type === 2 ? '下刊' : '中间监播',
                                arr: [
                                    {
                                        shoot_type: this.data.photoRequire.shoot_type,
                                        shoot_date: this.data.photoRequire.shoot_date,
                                        baidu_time: this.data.photoRequire.baidu_time,
                                        watermark_time: this.data.photoRequire.watermark_time,
                                        newspaper_time: this.data.photoRequire.newspaper_time,
                                        normal_shoot: this.data.photoRequire.normal_shoot,
                                        background_clear: this.data.photoRequire.background_clear,
                                        newpaper_clear: this.data.photoRequire.newpaper_clear,
                                        close_shoot_num: this.data.photoRequire.close_shoot_num,
                                        far_shoot_num: this.data.photoRequire.far_shoot_num,
                                        memo: this.data.photoRequire.memo
                                    }
                                ]
                            },
                        ],
                        activeNames: []
                    })
                }
                this.setData({
                    photoRequirementsDataArr: this.data.photoRequirementsDataArr,
                })
                this.onClose()
            }
        },
        validForm() {
            if (!this.data.validate.checkForm(this.data.cuForm)) {
                Toast.fail({
                    message: this.data.validate.errorList[0].msg,
                    context: this,
                    selector: '#custom-selector'
                })
                return false
            } else {
                // 检查拍照要求
                if (this.data.photoRequirementsDataArr.length > 0) {
                    if (this.data.photoRequirementsDataArr.length === 1) {
                        if (this.data.photoRequirementsDataArr[0].media_type_id !== null) {
                            return true
                        } else {
                            Toast.fail({
                                message: '系统异常，请联系管理员',
                                context: this,
                                selector: '#custom-selector'
                            })
                            return false
                        }
                    } else {
                        for (let i = 0; i < this.data.photoRequirementsDataArr.length; i++) {
                            let tmp = this.data.photoRequirementsDataArr[i];
                            if (tmp.media_type_id === -1) {
                                Toast.fail({
                                    message: '有全局适用的拍照要求，请勿给媒体类型单独添加',
                                    context: this,
                                    selector: '#custom-selector'
                                })
                                return false
                            }
                        }
                        return true
                    }
                } else {
                    Toast.fail({
                        message: '至少添加一项拍照要求',
                        context: this,
                        selector: '#custom-selector'
                    })
                    return false
                }
            }
        },
        save() {
            const that = this
            const req = JSON.parse(JSON.stringify(this.data.cuForm))
            req.mediaType2RequireList = {}
            this.data.photoRequirementsDataArr.forEach(item => {
                let arr = []
                item.requireList.forEach(tmp => {
                    arr = tmp.arr.concat(arr)
                })
                req.mediaType2RequireList[item.media_type_id + ''] = arr
            })
            util.showLoading('保存中')
            network.reqPromise('POST', {
                uri: app.api.billMarketSave,
                params: req
            }).then(res => {
                util.hideLoading()
                Toast.success({
                    message: '操作成功',
                    context: that,
                    selector: '#custom-selector'
                })
                setTimeout(() => {
                    this.back()
                }, 800)
            }).catch(() => {
                Toast.fail({
                    message: '操作失败',
                    context: that,
                    selector: '#custom-selector'
                })
                util.hideLoading()
            })
        },
        submit() {
            const that = this
            const req = JSON.parse(JSON.stringify(this.data.cuForm))
            req.mediaType2RequireList = {}
            this.data.photoRequirementsDataArr.forEach(item => {
                let arr = []
                item.requireList.forEach(tmp => {
                    arr = tmp.arr.concat(arr)
                })
                req.mediaType2RequireList[item.media_type_id + ''] = arr
            })
            util.showLoading('保存中')
            network.reqPromise('PUT', {
                uri: app.api.billMarketBegin,
                params: req,
            }).then(res => {
                util.hideLoading()
                Toast.success({
                    message: '操作成功',
                    context: that,
                    selector: '#custom-selector'
                })
                setTimeout(() => {
                    this.back()
                }, 800)
            }).catch(() => {
                Toast.fail({
                    message: '操作失败',
                    context: that,
                    selector: '#custom-selector'
                })
                util.hideLoading()
            })
        },
        pass(opinion) {
            const that = this
            util.showLoading('提交中')
            network.reqPromise('PUT', {
                uri: app.api.billMarketPass,
                params: {
                    id: that.data.cuForm.id,
                    wf_check_opinion: opinion.detail,
                },
            }).then(res => {
                util.hideLoading()
                Toast.success({
                    message: '操作成功',
                    context: that,
                    selector: '#custom-selector'
                })
                setTimeout(() => {
                    this.back()
                }, 800)
            }).catch(() => {
                Toast.fail({
                    message: '操作失败',
                    context: that,
                    selector: '#custom-selector'
                })
                util.hideLoading()
            })
        },
        returnBack(opinion) {
            const that = this
            util.showLoading('提交中')
            network.reqPromise('PUT', {
                uri: app.api.billMarketReturnBack,
                params: {
                    id: that.data.cuForm.id,
                    wf_check_opinion: opinion.detail,
                },
            }).then(res => {
                util.hideLoading()
                Toast.success({
                    message: '操作成功',
                    context: that,
                    selector: '#custom-selector'
                })
                setTimeout(() => {
                    this.back()
                }, 800)
            }).catch(() => {
                Toast.fail({
                    message: '操作失败',
                    context: that,
                    selector: '#custom-selector'
                })
                util.hideLoading()
            })
        },
        del() {
            const that = this
            util.showLoading('提交中')
            network.reqPromise('DELETE', {
                uri: app.api.billMarketDelete,
                params: {
                    id: that.data.cuForm.id
                },
            }).then(res => {
                util.hideLoading()
                Toast.success({
                    message: '操作成功',
                    context: that,
                    selector: '#custom-selector'
                })
                setTimeout(() => {
                    this.back()
                }, 800)
            }).catch(() => {
                Toast.fail({
                    message: '操作失败',
                    context: that,
                    selector: '#custom-selector'
                })
                util.hideLoading()
            })
        },
        actDel() {
            const that = this
            util.showLoading('提交中')
            network.reqPromise('DELETE', {
                uri: app.api.billMarketActualDelete,
                params: {
                    id: that.data.cuForm.id
                },
            }).then(res => {
                util.hideLoading()
                Toast.success({
                    message: '操作成功',
                    context: that,
                    selector: '#custom-selector'
                })
                setTimeout(() => {
                    this.back()
                }, 800)
            }).catch(() => {
                Toast.fail({
                    message: '操作失败',
                    context: that,
                    selector: '#custom-selector'
                })
                util.hideLoading()
            })
        },
        typeRadioChange(e) {
            const that = this
            if (this.data.cuForm.type == e.detail) {
                return
            }
            Dialog.confirm({
                title: '切换类型',
                message: '切换类型到丢失已经填写的数据 您确定切换？',
                context: that
            }).then(() => {
                this.setData({
                    cuForm: {
                        dept_id: null,
                        customer_id: null,
                        contract_id: null,
                        bill_sale_id: null,
                        need_sample: null,
                        latest_up_date: null,
                        latest_up_time: null,
                        latest_down_date: null,
                        latest_down_time: null,
                        first_time: null,
                        has_new_pic: null,
                        show_proxy_flag: null,
                        pic_need_recycle: null,
                        design_user_id: null,
                        memo: null,
                        mediaType2RequireList: {
                            "-1": [],
                            "2": [
                                {
                                    shoot_type: null,
                                    shoot_date: null,
                                    baidu_time: null,
                                    watermark_time: null,
                                    newspaper_time: null,
                                    normal_shoot: null,
                                    background_clear: null,
                                    newpaper_clear: null,
                                    close_shoot_num: null,
                                    far_shoot_num: null
                                }
                            ]
                        },
                        type: e.detail,
                        companyName: null,// 公司名称
                        customer_name: null,// 项目名称
                        contractName: null,// 合同名称
                        saleOrderName: null,// 销售合同名称
                        userName: null,// 用户名称
                    },
                    photoRequirementsDataArr: []
                })
            }).catch(() => {
                this.setData({
                    'cuForm.type': e.detail === 1 ? 2 : 1
                })
            });

        },
        back() {
            wx.navigateBack({
                    delta: 1
                }
            )
        },
    },
    observers: {
        marketOrderId: function (val) {
            if (val > 0) {
                util.showLoading('加载中')
                network.reqPromise('GET', {
                    uri: app.api.billMarketDetail,
                    params: {
                        id: val
                    },
                }).then(res => {
                    this.setData({
                        cuForm: res.data,
                        mustIncludeBillSaleId: res.data.bill_sale_id
                    })
                    for (let key in this.data.cuForm.mediaType2RequireList) {
                        let data = {
                            media_type_id: parseInt(key),
                            media_type_name: res.data.typeMap[parseInt(key)] ? res.data.typeMap[parseInt(key)] : '未知',
                            requireList: []
                        }
                        for (let i = 1; i < 4; i++) {
                            let filter = this.data.cuForm.mediaType2RequireList[key].filter(item => item.shoot_type === i)
                            if (filter && filter.length) {
                                let tmp = {
                                    shoot_type: i,
                                    name: i === 1 ? '上刊' : i === 2 ? '下刊' : '中间监播',
                                    arr: JSON.parse(JSON.stringify(filter))
                                }
                                data.requireList.push(tmp)
                            }
                        }
                        this.data.photoRequirementsDataArr.push(data)
                    }
                    this.setData({
                        photoRequirementsDataArr: this.data.photoRequirementsDataArr
                    })
                    // 获取合同待选项
                    const that = this
                    network.GET({
                        uri: app.api.contractListByCustomerId,
                        params: {customerId: res.data.customer_id},
                        success(r) {
                            r.data.forEach(item => {
                                item.name = item.part_a + '——' + item.contract_num
                            })
                            that.setData({
                                contractList: r.data,
                                selectContractLoading: false
                            })
                            var find = r.data.find(item => item.id === res.data.contract_id);
                            if (find) {
                                that.setData({
                                    'cuForm.contractName': find.name,
                                })
                            }
                            network.GET({
                                uri: app.api.saleOrderListByContractId,
                                params: {
                                    contractId: res.data.contract_id,
                                    mustIncludeBillSaleId: that.data.mustIncludeBillSaleId,
                                    type: that.data.cuForm.type
                                },
                                success(response) {
                                    response.data.forEach(item => {
                                        item.name = item.contract_num + '——' + item.actual_execute_time_start + '~' + item.actual_execute_time_end
                                    })
                                    that.setData({
                                        saleOrderList: response.data,
                                        selectSaleOrderLoading: false
                                    })
                                    var f = response.data.find(item => item.id === res.data.bill_sale_id);
                                    if (f) {
                                        that.setData({
                                            'cuForm.saleOrderName': f.name,
                                        })
                                    }
                                    util.hideLoading()
                                },
                                fail(e) {
                                    util.hideLoading()
                                }
                            })
                        },
                        fail(e) {
                            util.hideLoading()
                        }
                    })
                }).catch(() => {
                    Toast.fail({
                        message: '加载失败',
                        context: this,
                        selector: '#custom-selector'
                    })
                    util.hideLoading()
                })
            }
        }
    }
})
